<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://xiaoji.name All rights reserved.
// +----------------------------------------------------------------------
// | Author: jackie <297129853@qq.com>
// +----------------------------------------------------------------------
namespace plugins\juncdt_sms;

use cmf\lib\Plugin;
use think\Db;

class JuncdtSmsPlugin extends Plugin
{
    public $info = array(
        'name'=>'JuncdtSms',
        'title'=>'极云短信',
        'description'=>'极云短信',
        'status'=>1,
        'author'=>'',
        'version'=>'1.0'
    );
    
    public $has_admin=0;//插件是否有后台管理界面
    
    public function install()
    {//安装方法必须实现
    	$where_data1 = [
            'type' => 1,
            'once' => 1,
            'name' => '极云短信',
            'hook' => 'send_juncdt_sms',
            'app' => 'cmf',
            'description' => '极云短信'
        ];
        $test = Db::name('hook')->where($where_data1)->find();
        if (empty($test)) {
            Db::name('hook')->insert($where_data1);
        }
        return true;//安装成功返回true，失败false
    }
    
    public function uninstall()
    {//卸载方法必须实现
    	$where_data1 = [
            'type' => 1,
            'once' => 1,
            'name' => '极云短信',
            'hook' => 'send_juncdt_sms',
            'app' => 'cmf',
            'description' => '极云短信'
        ];
        $test = Db::name('hook')->where($where_data1)->find();
        if (!empty($test)) {
            Db::name('hook')->where($where_data1)->delete();
        }
        return true;//卸载成功返回true，失败false
    }

    public function sendJuncdtSms($param)
    {
        $url = 'https://market.juncdt.com/smartmarket/msgService/sendMessage';
        $config        = $this->getConfig();

        $data = [
            'accessKey'             => $config['accessKey'],
            'accessSecret'          => $config['accessSecret'],
            'classificationSecret'  => $config['classificationSecret'],
            'signCode'              => $config['signCode'],
            'templateCode'          => $config['templateCode'],
            'phone'                 => $param['mobile'],
            'params'                => ['code'=>$param['code']],
        ];

        //curl请求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));//$data JSON类型字符串
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($data))));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output,true);
    }

}